package com.cdd.goods.service.impl;

import com.cdd.goods.service.INewgoodsService;

import com.common.untils.CommonResult;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.goods.mapper.GoodsMapper;
import com.cdd.goods.mapper.NewgoodsMapper;
import com.cdd.goods.service.INewgoodsService;


import com.common.entity.Goods;
import com.common.entity.GoodsBrand;
import com.common.entity.Newgoods;
import com.common.untils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-09
 */
@Service
public class NewgoodsServiceImpl implements INewgoodsService {



@Autowired
private NewgoodsMapper newgoodsMapper;
@Autowired
private GoodsMapper goodsMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;



    @Override
    public CommonResult selectAll(Integer currentPage,Integer pageSize) {
     Page<Newgoods> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Newgoods> wrapper=new QueryWrapper();
         wrapper.eq("n.is_deleted",0);
         wrapper.eq("start",2);
        IPage<Map> iPage = newgoodsMapper.selectAll(page, wrapper);
       // System.out.println(iPage.getRecords()+"-------------------------------------");
        return new CommonResult(2000,"查询成功",iPage);

    }

    @Override
    public CommonResult pass(Goods goods,String token) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String username = user.getString("username");

        QueryWrapper<Newgoods> wrapper=new QueryWrapper();
        Newgoods newgoods=new Newgoods();
        wrapper.eq("id",goods.getId());
        newgoods.setStart(0);
        newgoods.setUname(username);
        List<Goods> goods2 = newgoodsMapper.selectGname();

        for (Goods g:goods2) {
            System.out.println(g.getGname()+"222222222222222");
            System.out.println(goods.getGname()+"33333333333333333333333");
            if (g.getGname().equals(goods.getGname())) {
                System.out.println("__________________________________");
                return new CommonResult(5000, "通过失败，已有该商品", null);
            }
        }
        int update = newgoodsMapper.update(newgoods, wrapper);
        System.out.println(update+"iiiiiiiiiiiiiiii");
        if (update==1) {
            Newgoods newgoods1 = newgoodsMapper.selectById(goods.getId());
            Goods goods1 = new Goods();
            goods1.setId(newgoods1.getId());
            goods1.setTypeId(newgoods1.getTypeId());
            goods1.setGname(newgoods1.getGname());
            goods1.setImg(newgoods1.getImg());
            goods1.setDescription(newgoods1.getDescription());
            goods1.setGsid(newgoods1.getGsid());
            goods1.setGbid(newgoods1.getGbid());
            goods1.setGdid(newgoods1.getGdid());
            goods1.setCode(newgoods1.getCode());
            goods1.setIsDeleted(newgoods1.getIsDeleted());
            goods1.setGmtCreate(newgoods1.getGmtCreate());
            goods1.setGmtModified(newgoods1.getGmtModified());
            int insert = goodsMapper.insert(goods1);
            return new CommonResult(2000, "通过审核", null);
        }


        return new CommonResult(5000,"通过失败",null);
    }

    @Override
    public CommonResult addNewGoods(Newgoods newgoods) {
        newgoods.setGmtCreate(LocalDateTime.now());
        newgoods.setGmtModified(LocalDateTime.now());
        Integer row = newgoodsMapper.insert(newgoods);
        if(row==1){
            return new CommonResult(2000,"添加成功",null);
        }else {
            return new CommonResult(5000,"添加失败",null);
        }
    }

    @Override
    public CommonResult reject(String id, Newgoods newgoods,String token) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String username = user.getString("username");

        System.out.println(newgoods+"--------------");
        QueryWrapper<Newgoods> wrapper=new QueryWrapper();
        wrapper.eq("id",id);

        newgoods.setStart(1);
        newgoods.setUname(username);
       newgoodsMapper.update(newgoods, wrapper);
        return new CommonResult(5000,"驳回成功",null);
    }

    @Override
    public CommonResult audit(Integer currentPage, Integer pageSize,Newgoods newgoods) {
        Page<Newgoods> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Newgoods> wrapper=new QueryWrapper();
        wrapper.eq("n.is_deleted",0);
        if (StringUtils.hasText(newgoods.getGname())){
            wrapper.like("gname",newgoods.getGname());
        }
        if (newgoods.getIsDeleted()!=null){
            wrapper.eq("is_deleted",newgoods.getIsDeleted());
        }
        IPage<Map> iPage = newgoodsMapper.selectAllauto(page, wrapper);

        return new CommonResult(2000,"查询成功",iPage);
    }
}
